package com.jyw.business.obstacles.domain.vo.web;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.jyw.business.obstacles.constant.OrderMakeTypeConstant;
import com.jyw.common.constant.SysDictTypeConstant;
import com.jyw.business.obstacles.domain.ObsOrderMake;
import com.jyw.business.obstacles.domain.ObsOrderProcess;
import com.jyw.common.annotation.Conversion;
import com.jyw.common.annotation.Excel;
import com.jyw.common.enums.ConversionType;
import com.jyw.common.utils.DictUtils;
import com.jyw.common.utils.bean.BeanUtils;
import com.jyw.system.domain.vo.SimpleFileVo;
import com.jyw.system.utils.DeptUtils;
import com.jyw.system.utils.FileUtils;
import com.jyw.system.utils.TeamUtils;
import com.jyw.system.utils.UserUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.FieldNameConstants;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 沈峻
 * @ClassName MakeProcessVO
 * @Description 处理记录
 * @Date 2023/8/17 13:56
 **/
@ApiModel("处理记录")
@Data
@NoArgsConstructor
@FieldNameConstants
public class MakeProcessVO {

    /** 当前处理人员 */
    @ApiModelProperty("处理人员")
    @Excel(name = "处理人员")
    @Conversion(value = ConversionType.USER,name = Fields.makeUserName)
    private Long makeUser;

    @ApiModelProperty("处理人员名称")
    private String makeUserName;

    /** 完成时间 */
    @ApiModelProperty("处理时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm")
    private Date operDate;

    @ApiModelProperty("处理类型")
    @Conversion(value = ConversionType.DICT,dictType = SysDictTypeConstant.OBS_MAKE_TYPE)
    private String makeType;

    @ApiModelProperty("处理类型标签")
    private String makeTypeToName;

    @ApiModelProperty("处理内容")
    private String processValue;

    @ApiModelProperty("处理内容对象")
    private Object processObj;

    @ApiModelProperty("处理附件")
    private List<SimpleFileVo> processEnclosure;


    public MakeProcessVO(ObsOrderProcess data) {
        //处理人
        setMakeUser(data.getCreateUser());
        //处理时间
        setOperDate(data.getCreateTime());
        //处理类型
        setMakeType(data.getMakeState());
        //设置处理内容
        setProcessValue(data.getProcessValue());
        if(data.getProcessValue()!=null){
            if(getMakeType().equals(OrderMakeTypeConstant.MAKING)){
                // 处理
                JSONObject json = JSONObject.parseObject(data.getProcessValue());
                setProcessValue(json.toJSONString());
                setProcessObj(json.to(Map.class));
            }else if(getMakeType().equals(OrderMakeTypeConstant.COMPLETE)){
                // 完成
                JSONObject json = JSONObject.parseObject(data.getProcessValue());
                if(json.getInteger("actualRepairPlace")!=null){
                    //报修地址
                    json.put("actualRepairPlaceToName",DictUtils.getDictLabel(SysDictTypeConstant.OBS_REPAIR_PLACE,json.getInteger("actualRepairPlace").toString()));
                }
                if(json.getString("faultType")!=null){
                    //故障分类
                    json.put("faultTypeToName",DictUtils.getDictLabel(SysDictTypeConstant.OBS_FAULT_TYPE,json.getString("faultType")));
                }
                if(json.getString("equipmentAddressId")!=null){
                    // 资产位置
                    json.put("equipmentAddress", DeptUtils.deptIdToName(json.getLong("equipmentAddressId")));

                }
                setProcessValue(json.toJSONString());
                setProcessObj(json.to(Map.class));
            }else if(getMakeType().equals(OrderMakeTypeConstant.SUSPEND)){
                //暂停
                JSONObject json = JSONObject.parseObject(data.getProcessValue());
                json.put("reason", DictUtils.getDictLabel(SysDictTypeConstant.OBS_PAUSE_REASON,json.getString("reason")));
                setProcessValue(json.toJSONString());
                setProcessObj(json.to(Map.class));
            }
            else if(getMakeType().equals(OrderMakeTypeConstant.END_TERMINATION)){
                JSONObject json = JSONObject.parseObject(data.getProcessValue());
                //终止
                json.put("reason", DictUtils.getDictLabel(SysDictTypeConstant.OBS_TERMINATION_REASON,json.getString("reason")));
                setProcessValue(json.toJSONString());
                setProcessObj(json.to(Map.class));
            }else if(getMakeType().equals(OrderMakeTypeConstant.FORWARD)){
                JSONObject json = JSONObject.parseObject(data.getProcessValue());
                //转单
                json.put("reason", DictUtils.getDictLabel(SysDictTypeConstant.OBS_TRANS_REASON,json.getString("reason")));
                //转单人
                Integer sendObjType = json.getInteger("sendObjType");
                switch (sendObjType){
                    case 1:
                        json.put("sendName", UserUtils.userIdToName(json.getLong("sendObjId")));
                        break;
                    case 2:
                        json.put("sendName", TeamUtils.teamIdToName(json.getLong("sendObjId")));
                        break;
                    case 3:
                        json.put("sendName", DeptUtils.deptIdToName(json.getLong("sendObjId")));
                        break;
                }
                setProcessValue(json.toJSONString());
                setProcessObj(json.to(Map.class));
            }else if (getMakeType().equals(OrderMakeTypeConstant.BACK)){
                JSONObject json = JSONObject.parseObject(data.getProcessValue());
                //退回
                json.put("reason", DictUtils.getDictLabel(SysDictTypeConstant.OBS_RETURN_RECEPTION,json.getString("reason")));
                setProcessValue(json.toJSONString());
                setProcessObj(json.to(Map.class));
            } else if (getMakeType().equals(OrderMakeTypeConstant.END_UPGRADE)) {
                //todo 工单升级
                JSONObject json = JSONObject.parseObject(data.getProcessValue());
                // 优先级
                json.put("levelToName", DictUtils.getDictLabel(SysDictTypeConstant.OBS_PRIORITY_LEVEL,json.getString("level")));
                // 涉及系统
                json.put("aboutSystemToName", DictUtils.getDictLabel(SysDictTypeConstant.OBS_ABOUT_SYSTEM,json.getString("aboutSystem")));
                // 提出科室
                if(json.getJSONArray("proposeList")!=null){
                    JSONArray proposeList = json.getJSONArray("proposeList");
                    for (int i = 0; i < proposeList.size(); i++) {
                        JSONObject jsonObject = proposeList.getJSONObject(i);
                        jsonObject.put("organizerName", DeptUtils.deptIdToName(proposeList.getJSONObject(i).getLong("organizer")));
                    }
                }

                setProcessValue(json.toJSONString());
                setProcessObj(json.to(Map.class));
            }else if (getMakeType().equals(OrderMakeTypeConstant.ASSIST)) {
                JSONObject json = JSONObject.parseObject(data.getProcessValue());
                JSONArray assistList = json.getJSONArray("assistList");
                for (int i = 0; i < assistList.size(); i++) {
                    JSONObject jsonObject=assistList.getJSONObject(i);
                    Integer sendObjType = jsonObject.getInteger("sendObjType");
                    switch (sendObjType){
                        case 1:
                            jsonObject.put("sendName", UserUtils.userIdToName(assistList.getJSONObject(i).getLong("sendObjId")));
                            break;
                        case 2:
                            jsonObject.put("sendName", TeamUtils.teamIdToName(assistList.getJSONObject(i).getLong("sendObjId")));
                            break;
                        case 3:
                            jsonObject.put("sendName", DeptUtils.deptIdToName(assistList.getJSONObject(i).getLong("sendObjId")));
                            break;
                    }
                }
                setProcessValue(json.toJSONString());
                setProcessObj(json.to(Map.class));

            }else if (getMakeType().equals(OrderMakeTypeConstant.END_UPGRADE)) {
                JSONObject json = JSONObject.parseObject(data.getProcessValue());
                //报修地址
                json.put("actualRepairPlaceToName",DictUtils.getDictLabel(SysDictTypeConstant.OBS_REPAIR_PLACE,json.getInteger("actualRepairPlace").toString()));
                //故障分类
                json.put("faultTypeToName",DictUtils.getDictLabel(SysDictTypeConstant.OBS_FAULT_TYPE,json.getString("faultType")));
                setProcessValue(json.toJSONString());
                setProcessObj(json.to(Map.class));
            }
        }


        setProcessEnclosure(FileUtils.IdToFile(data.getProcessEnclosure()));
    }

    public MakeProcessVO(ObsOrderMake make){
        BeanUtils.copyProperties(make,this);
    }
}
